package cn.shengongying.admin.controller;

import cn.shengongying.admin.dto.CircleArticleListInDTO;
import cn.shengongying.bean.DataTablePageResult;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.dubbo.modal.CircleArticleList;
import cn.shengongying.dubbo.modal.CircleList;
import cn.shengongying.dubbo.service.ICircleArticleListService;
import cn.shengongying.dubbo.service.ICircleListService;
import cn.shengongying.model.Information;
import cn.shengongying.service.IInformationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * author: RXK
 * date: 2022/2/19 星期六
 * desc:
 */
@Controller
@RequestMapping("/sys/circle/article/list")
public class CircleListArticleController extends BaseController{


    @DubboReference(version = ICircleListService.version)
    private ICircleListService circleListService;

    private final IInformationService informationService;

    @DubboReference(version = ICircleArticleListService.version,injvm = false)
    private ICircleArticleListService circleArticleListService;

    public CircleListArticleController(IInformationService informationService) {
        this.informationService = informationService;
    }


    @RequestMapping("/index")
    public String index() {
        return "/circle_article/index";
    }

    @ResponseBody
    @RequestMapping("/list")
    public DataTablePageResult<List<CircleArticleList>> list(@RequestBody CircleArticleListInDTO inDTO) {
        List<CircleArticleList> list = circleArticleListService.list();
        inDTO.setData(list);
        long count = circleArticleListService.count();
        inDTO.setRecordsTotal(count);
        return inDTO;
    }


    @ResponseBody
    @RequestMapping("/detail")
    public ResponseResult<CircleList> detail(@RequestBody CircleArticleListInDTO inDTO) {
        CircleList bannerList = circleListService.getById(inDTO.getId());
        return ResponseResult.success(bannerList);
    }


    @ResponseBody
    @RequestMapping("/update/status")
    public ResponseResult<String> updateStatus(@RequestBody CircleArticleListInDTO inDTO) {
        CircleArticleList circleList = new CircleArticleList();
        circleList.setId(inDTO.getId());
        if (inDTO.getUpdateType() == 1) {
            circleList.setQuintessence(inDTO.getStatus());
        }

        if (inDTO.getUpdateType() == 2) {
            circleList.setRecommend(inDTO.getStatus());
        }

        if (inDTO.getUpdateType() == 3) {
            circleList.setTag(inDTO.getStatus());
        }

        if (inDTO.getUpdateType() == 4) {
            circleList.setTopStatus(inDTO.getStatus());
        }

        circleArticleListService.updateById(circleList);
        return ResponseResult.success();
    }

    @ResponseBody
    @RequestMapping("/delete")
    public ResponseResult<String> delete(@RequestBody CircleArticleListInDTO inDTO) {
        CircleArticleList circleArticleList = circleArticleListService.getById(inDTO.getId());
        circleArticleListService.removeById(inDTO.getId());
        Long userId = circleArticleList.getUserId();
        Information information = userMsg(userId, inDTO.getMsg());
        informationService.insert(information);
        return ResponseResult.success();
    }

    private Information userMsg(Long userId, String msg) {
        Information information = new Information();
        information.setUserId(userId);
        information.setType(5);
        information.setContent(msg);
        information.setRead(0);
        information.setFrom(0L);
        return information;
    }




}
